package com.cdelink.xzgy.ftp.user.controller;

import com.cdelink.xzgy.ftp.annotation.Authorized;
import com.cdelink.xzgy.ftp.common.ReturnData;
import com.cdelink.xzgy.ftp.common.page.PageRequest;
import com.cdelink.xzgy.ftp.common.page.PageResponse;
import com.cdelink.xzgy.ftp.user.entity.Records;
import com.cdelink.xzgy.ftp.user.entity.User;
import com.cdelink.xzgy.ftp.user.entity.dto.LoginDto;
import com.cdelink.xzgy.ftp.user.entity.dto.UserDto;
import com.cdelink.xzgy.ftp.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author sxl
 * @since 2021-12-13 17:14:21
 */
@Api(tags = "用户")
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    /**
     * 用户登录
     *
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "用户登录接口")
    @PostMapping("session")
    public ReturnData login(@RequestBody LoginDto loginDto) {
        Map<String, Object> login = userService.login(loginDto);
        return ReturnData.ok(login);
    }

    /**
     * 用户注销
     *
     * @return
     */
    @Authorized
    @ApiOperation(value = "用户注销", notes = "用户注销接口")
    @DeleteMapping("session")
    public ReturnData logout(@ApiIgnore @RequestAttribute Integer userId) {
        userService.logout(userId);
        return ReturnData.ok("退出登录成功");
    }


    /**
     * 用户分页
     *
     * @param pageRequest
     * @return
     */
    @ApiOperation(value = "用户分页", notes = "用户分页",hidden = true)
    @PostMapping("/page")
    public ReturnData getPage(@RequestBody PageRequest<UserDto> pageRequest) {
        PageResponse<User> page = userService.getPage(pageRequest);
        return ReturnData.ok(page);
    }

    /**
     * 查询用户操作记录
     *
     * @return
     */
    @ApiOperation(value = "文件操作记录列表", notes = "文件操作记录列表")
    @Authorized
    @GetMapping("/operateRecords")
    public ReturnData getPage(@RequestParam(required = false) Integer pageIndex, @RequestParam(required = false) Integer pageSize) {
        PageRequest<Object> pageRequest = new PageRequest<>(pageIndex, pageSize);
        PageResponse<Records> page = userService.getPageRecords(pageRequest);
        return ReturnData.ok(page);
    }


}
